#!/usr/bin/python
# author: yoshiii.yoshiii@googlemail.com
# you found this code on http://yoshiii-box.googlecode.com/svn/ 
#
import datetime, Tkinter, time, threading

start = datetime.datetime(2006, 4, 1, 9)
goal = datetime.datetime(2009, 3, 31, 18)

def update(start, goal, tknow, tkexact, tkapprox):
    while not exit_thread:
        now  = datetime.datetime.now()
        diff = goal - now
        tknow.set('now: '+ str(now).split('.', 1)[0])
        tkexact.set('left: '+ str(diff).split('.', 1)[0])

	# approximate
	months = float(diff.days) / (365.0/12)
	fraction = 100 * float(diff.days) / (goal - start).days
        tkapprox.set('ca. %.1f months '%months + ' ~ %.2f%%'%(fraction))
        time.sleep(1)

def main():
    global exit_thread
    exit_thread = False
    
    root = Tkinter.Tk()
    root.title('keine Panik!')
    now    = Tkinter.StringVar()
    exact  = Tkinter.StringVar()
    approx = Tkinter.StringVar()
           
    Tkinter.Label(root, text='start: '+str(start)).pack()
    Tkinter.Label(root, text='goal:  '+str(goal)).pack()
    Tkinter.Label(root, textvar = now).pack()
    Tkinter.Label(root, textvar = exact).pack()
    Tkinter.Label(root, textvar = approx).pack()

    threading.Thread(target=update, args=(start, goal, now, exact, approx)).start()
    
    root.mainloop()
    exit_thread = True
    while threading.activeCount() > 2:
        time.sleep(1)

if __name__ == '__main__':
    main()
